proc format;
value $cd
      "Chewing Tobacco" = "CHTB"                      
      "Cigar(s)" = "CGR"                             
      "Cigarette(s)"="CGRT"                           
      "Cigarillo(s)" ="CGRLO"                          
      "Electronic Cigarette(s)/E-Vapors" = "ECGRT"     
      "Nicotine Product(s): (patch, gum, etc)"="NICP"
;

invalue mn
     "Chewing Tobacco"=1                         
     "Cigar(s)" =2                              
     "Cigarette(s)" =3                          
     "Cigarillo(s)"=4                           
     "Electronic Cigarette(s)/E-Vapors"=5       
     "Nicotine Product(s): (patch, gum, etc)"=6
;
quit; 


%get_data(pre=sdtm,lib=sdtm,supp=y,lst=su);
/*
proc contents data=sdtm.su short;
run;*/
/*proc sort data=sdtm.su out=suu nodupkey;by sutrt;run;proc print;run;*/

/*SUCAT  SUDOSFRM SUDOSFRQ SUDOSU SUENRTPT SUENTPT SUSEQ SUSTDTC SUSTDY SUTRT; */

data su ;/*(keep=studyid usubjid sutrt  srcdom srcseq */
/*sudosfrq sudosu  param paramcd Aval Avalc paramn sustdtc suendtc);*/
   set sdtmsu;
    by usubjid;
    srcseq=suseq;
    srcdom=domain;
	if ~missing(sudosu) then param= strip(sutrt)||" ("||strip(sudosu)||")";
	else param=strip(sutrt);
    paramcd=strip(put(sutrt,cd.));
    parcat1=sucat;
    if ~missing(sucat) then parcat1n=1;
    paramn=input(sutrt,mn.);
     aval= sudose;
    avalc=strip(put(sudose,best.));
    Avisit=visit;
    avisitn=visitnum;
/*    if paramcd="CPD" then paramn=1;*/
/*    if ~missing(sustdtc)  then adt= input(sustdtc, 4.);*/
run;

data dsu(drop=arm armcd actarm actarmcd);

 merge su (in=a) adam.adsl(in=b);
 by usubjid;
 if a and b ;
/* if ~missing(consendt) then icfyr = year(consendt);*/
/* if ~missing(SUSTDTC) then suyr = input(substr(SUSTDTC,1,4),4.);*/
/*/* if missing(SUSTDTC) then suyr = SUSTDTC;*/*/
/* if nmiss(icfyr,suyr) = 0 then do ;*/
/*      smokeyrs = put((icfyr - suyr)+1,2.) ;*/
/*  end;*/
  
    if b then do;
   trtp=trt01p;
   trta=trt01a;
   trtpn=trt01pn;
	trtan=trt01an;
/*   end;*/
 
run;

 data fin;
 set DSU;
 if ~missing(sustdtc) then do;
 if length(sustdtc)=4 then sustdtc_=strip(sustdtc)||"-"||"01"||"-"||"01";
 if length(sustdtc)=7 then sustdtc_=strip(sustdtc)||"-"||"01";
  if length(sustdtc)=10 then sustdtc_=strip(sustdtc);

end;
/* if missing(suendtc) then suendtc_=strip(rficdtc);*/
 if ~missing(suendtc) then do;
 if length(suendtc)=4 then suendtc_=strip(suendtc)||"-"||"01"||"-"||"01";
 if length(suendtc)=7 then suendtc_=strip(suendtc)||"-"||"01";
  if length(suendtc)=10 then suendtc_=strip(suendtc);

end;
 if missing(suendtc) then instatus="CURRENT";
  else instatus="FORMER";
/*keep usubjid sustdtc sustdtc_ suendtc suendtc_ instatus;*/
run;



proc sort data=sdtm.dm out=dm(keep=usubjid RFICDTC);by usubjid;run;
proc sort data=fin out=fin1;by usubjid;run;

data final ;
merge dm(in=a) fin1(in=b);
by usubjid;
if a and b;
 if missing(suendtc) then suendtc_=strip(RFICDTC);
run;
data fin2;
set final;
if ~missing(sustdtc_) and ~missing(suendtc_) then 
adur=round(((input(suendtc_,??yymmdd10.)-input(sustdtc_,??yymmdd10.)+1)/365.25),0.01);
duration_=intck("years",input(sustdtc_,??yymmdd10.),input(suendtc_,??yymmdd10.));
aduru="YEARS";
/*keep duration duration_;*/
run;


data fin2;
 label 
        srcdom="Source Data"
        Srcseq="Source Sequence" 
        param="Parameter"
        paramcd="Parameter Code"
        paramn="Parameter (N)"
        parcat1="Parameter Category 1" 
        parcat1n="Parameter Category 1 (N)"
        aval="Analysis Value"
        avalc="Analysis Value (C)"
        TRTP="Planned Treatment"
        TRTPN="Planned Treatment (N)"
        TRTA="Actual Treatment"
        TRTAN="Actual Treatment (N)"
        INSTATUS="Smoking Intake Status"
        ADUR="Substance use Duration"
        ADURU="Duration Units"
        avisit="Analysis Visit"
        avisitn="Analysis Visit (N)"
        ;
   set fin2;
 run;

 proc sql; 
  create table adamw.adsu (label="Substance Use Analysis Dataset") as
    select studyid,usubjid,subjid,siteid,complfl,saffl,trtsdt,trtedt,trtp,trtpn,
           trta,trtan,trt01p,trt01pn,trt01a,trt01an,paramcd,param,paramn,parcat1,parcat1n,aval,avalc,sudosfrq,
           avisit,avisitn,epoch,sustdtc,suendtc,/*sustdy,*/ suendy ,adur,aduru,instatus
    from fin2
    order by studyid,usubjid,paramn;
quit;

proc contents data=adamw.adsu;
run;


/*proc datasets lib=work kill nolist nodetails;*/
/*run;*/
%clrw;
